/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.Address;
import org.ipti.br.tag.entity.Address_;
import org.ipti.br.tag.entity.City;
import org.ipti.br.tag.entity.City_;
import org.ipti.br.tag.entity.Neighborhood;
import org.ipti.br.tag.entity.Neighborhood_;
import org.ipti.br.tag.entity.Person;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 *
 * @author israel
 */
@Stateless
public class NeighborhoodFacade extends AbstractFacade<Neighborhood> {
    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public NeighborhoodFacade() {
        super(Neighborhood.class);
    }
    
    @Override
    public void create(Neighborhood neighborhood) {
        getEntityManager().persist(neighborhood);
    }
    
    public List<Neighborhood> findAllOrder() {
        return findAllOrderBy(Neighborhood_.name);
    }
    
    public Neighborhood searchByNameCity(String name, Long cityID) {
        try {
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<Neighborhood> cq = cb.createQuery(Neighborhood.class);
            Root<Neighborhood> rt = cq.from(Neighborhood.class);
            cq.where(cb.and(
                    cb.like(cb.upper(rt.get(Neighborhood_.name)), name.toUpperCase()),
                    cb.equal(rt.join(Neighborhood_.city).get(City_.id), cityID))
                    );
            return getEntityManager().createQuery(cq).getSingleResult();
        } catch(Exception e) {
            return null;
        }
    }
    
    public List<String> neighborhoodList(String name, City city) {
        try {
            CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
            CriteriaQuery<String> cq = cb.createQuery(String.class);
            Root<Neighborhood> rt = cq.from(Neighborhood.class);
            cq.select(rt.get(Neighborhood_.name));
            cq.where(cb.and(
                    cb.like(cb.upper(rt.get(Neighborhood_.name)), "%"+ name.toUpperCase() +"%"),
                    cb.equal(rt.join(Neighborhood_.city).get(City_.id), city.getId()))
                    )
                    .orderBy(cb.asc(rt.get(Neighborhood_.name)));
            Query q = em.createQuery(cq).setMaxResults(10);
            return q.getResultList();
        } catch(Exception e) {
            return null;
        }
    }
    
    public List<Neighborhood> modifiedNeighborhoodBetweenDate(Calendar initialDate, Calendar finalDate, Long serialNumber) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Neighborhood> cq = cb.createQuery(Neighborhood.class);
        Root<Neighborhood> from = cq.from(Neighborhood.class);
        Predicate predicate1 = cb.notEqual(from.get(Neighborhood_.serialNumber), serialNumber);
        Predicate predicate2 = cb.greaterThanOrEqualTo(from.get(Neighborhood_.lastModified), initialDate);
        Predicate predicate3 = cb.lessThan(from.get(Neighborhood_.lastModified), finalDate);
        cq.where(cb.and(predicate1, predicate2, predicate3));

        return getEntityManager().createQuery(cq).getResultList();
    }
    
    public List<Address> getAddresses(Neighborhood neighborhood){
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Address> cq = cb.createQuery(Address.class);
        Root<Address> rt = cq.from(Address.class);
        cq.where(cb.equal(rt.get(Address_.neighborhood).get(Neighborhood_.id), neighborhood.getId()));

        return getEntityManager().createQuery(cq).getResultList();
    }
}
